package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.base.entity.HospitalConfig;
import com.scs.application.modules.base.mapper.HospitalConfigMapper;
import com.scs.application.modules.base.request.HospitalConfigQueryRequest;
import com.scs.application.modules.base.service.HospitalConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class HospitalConfigServiceImpl extends BaseServiceImpl<HospitalConfigMapper, HospitalConfig> implements HospitalConfigService {

    @Override
    public List<HospitalConfig> list(HospitalConfigQueryRequest request) {
        List<HospitalConfig> list = this.list(getQueryWrapper(request));
        return list;
    }

    @Override
    public IPage<HospitalConfig> page(HospitalConfigQueryRequest request) {
        IPage page = this.page(request.getPage(), getQueryWrapper(request));
        return page;
    }

    private QueryWrapper<HospitalConfig> getQueryWrapper(HospitalConfigQueryRequest request){
        return Wrappers.<HospitalConfig>query()
                .orderByDesc("gmt_modified");
    }

    @Override
    public HospitalConfig findByHospitalId(String hospitalId) {
        List<HospitalConfig> hospitalConfigs = this.list(Wrappers.<HospitalConfig>query().eq("hospital_id", hospitalId));
        if (hospitalConfigs != null && hospitalConfigs.size() > 0) {
            HospitalConfig hospitalConfig= new HospitalConfig();
            BeanUtils.copyProperties(hospitalConfigs.get(0), hospitalConfig);
            return hospitalConfig;
        }
        return null;
    }

    @Override
    public List<HospitalConfig> getAllHospitalConfigs() {
        return this.list();
    }
}

